﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Diagnostics;

namespace YGame.Base
{
	public sealed class DllLoadHelper
	{
		private DllLoadHelper()
		{
		}


		public static Dictionary<string, string> GetArgsDictionary(string[] args)
		{
			Dictionary<string, string> serverArgs = new Dictionary<string, string>();
			foreach (var item in args)
			{
				string key = "", value = "";
				string[] keyvalue = item.Split('=');

				if (keyvalue.Length >= 1)
				{
					key = keyvalue[0];
				}
				if (keyvalue.Length >= 2)
				{
					value = keyvalue[1];
				}
				serverArgs[key] = value;

			}

			return serverArgs;
		}

		static Dictionary<string, Assembly> models;
		static Dictionary<string, IGameModel> interfaces;

		public static object LoadMainDll(string path, string interfaceName)
		{
			Assembly assembly = Assembly.LoadFile(path);
			foreach (Type t in assembly.GetTypes())
			{
				// 检查基类是否为我们的模块接口
				if (t.GetInterface(interfaceName) != null)
				{
					return Activator.CreateInstance(t);
				}
			}
			return null;
		}



		public static void LoadModelDll(string path, IGameServerProvider gameProvider)
		{
			string interfaceName = "YGame.Base.IGameModel";
			//byte[] filedata = File.ReadAllBytes(path);
			Assembly assembly = Assembly.LoadFile(path);
			foreach (Type t in assembly.GetTypes())
			{
				// 检查基类是否为我们的模块接口
				if (t.GetInterface(interfaceName) != null)
				{
					IGameModel model = Activator.CreateInstance(t) as IGameModel;

					// 先检查是否已经加载过，有就先卸载
					if (interfaces.ContainsKey(t.FullName))
					{
						IGameModel oldMode = interfaces[t.FullName];
						Trace.Write(string.Format("unload model by: {0} \r\nclass: {1}\r\n", path, t.FullName));
						oldMode.Uninstall();
					}

					Trace.Write(string.Format("load model: {0} \r\nclass: {1}\r\n", path, t.FullName));
					model.Install(gameProvider);
					interfaces[t.FullName] = model;
				}
			}
		}


		public static void Close()
		{
			foreach (KeyValuePair<string, IGameModel> kvp in interfaces)
			{
				kvp.Value.Uninstall();
			}
		}


		//static void Load()
		//{
		//    AppDomainSetup setup = new AppDomainSetup();
		//    setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
		//    setup.ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
		//    setup.ShadowCopyFiles = "true";
		//    setup.ShadowCopyDirectories = "需要动态加载的Assembly所在文件夹";//影像副本目录

		//    setup.ApplicationName = "Dynamic";
		//    // Set up the Evidence
		//    System.Security.Policy.Evidence baseEvidence = AppDomain.CurrentDomain.Evidence;
		//    System.Security.Policy.Evidence evidence = new System.Security.Policy.Evidence(baseEvidence);


		//    // Create the AppDomain      
		//    AppDomain appDomain = AppDomain.CreateDomain("newDomain", evidence, setup);
		//    appDomain.CreateInstanceAndUnwrap("程序的AssemblyName", "那个辅助类的全名");
		//}

		//static void Unload()
		//{
		//    //AppDomain.Unload(appDomain);
		//}

	}
}
